package com.ldg.cart.controller;

import com.ldg.cart.client.ManagerClient;
import com.ldg.cart.entity.ItemEntity;
import com.ldg.cart.entity.group.Cart;
import com.ldg.cart.service.CartService;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-10-17-14:37
 * @Description:
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ManagerClient managerClient;
    @Autowired
    private CartService cartService;

    @RequestMapping("/addCart/{sellerId}/{num}")
    public String cart(@PathVariable("sellerId") String sellerId, @PathVariable("num") int num, Model model, HttpSession session) {        // 参数1：sku商品id 参数2：购买的商品数量
        // 得到登录名
        Assertion assertion = (Assertion) session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        if(assertion != null) {
            String name = assertion.getPrincipal().getName();
            model.addAttribute("name", name);
            List<Cart> carts = cartService.addCart(name, sellerId, num);
            model.addAttribute("cartList",carts);
        }
        return "cart";
    }

    @RequestMapping("/item/findById/{id}")
    @ResponseBody
    public ItemEntity findItem(@PathVariable String id){
        return managerClient.findById(id);

    }
}
